package org.micode.libs.dao.helper;

import org.micode.common.utils.DateUtils;
import org.micode.libs.dao.vo.StatDateQry;
import org.micode.libs.dao.vo.StatDateVO;

import java.util.Date;

import static org.micode.common.utils.DateUtils.date2Str;
import static org.micode.common.utils.DateUtils.monthFirstDate;
import static org.micode.common.utils.DateUtils.nextDate;
import static org.micode.common.utils.DateUtils.str2Date;

public class StatDateHelper {

    /**
     * 指定日期的时间区间
     *
     * @param oneDate YYYY-MM-DD
     * @return  {YYYY-MM-DD 00:00:00.000, next day YYYY-MM-DD 00:00:00.000 }
     */
    public static StatDateVO getDateInterval(String oneDate) {
        Date theDate = str2Date(oneDate, "yyyy-MM-dd");
        if (null != theDate) {
            String dateZeroHour = date2Str(theDate, "yyyy-MM-dd") + " 00:00:00.000";
            Date theNext = nextDate(theDate);
            String date24Hour = date2Str(theNext, "yyyy-MM-dd") + " 00:00:00.000";
            StatDateVO ret = new StatDateVO();
            ret.setBeginDate(dateZeroHour);
            ret.setEndDate(date24Hour);
            return ret;
        }
        return null;
    }

    /**
     * 指定月份的时间区间
     *
     * @param oneMonth YYYY-MM
     * @return  {YYYY-MM-01 00:00:00.000, next month YYYY-MM-01 00:00:00.000 }
     */
    public static StatDateVO getMonthInterval(String oneMonth) {
        Date theDate = str2Date(oneMonth + "-01", "yyyy-MM-dd");
        if (null != theDate) {
            String monthFirst = DateUtils.monthFirstDateStr(theDate) + " 00:00:00.000";
            String nextMonthFirst = DateUtils.nextMonthFirstDateStr(theDate) + " 00:00:00.000";
            StatDateVO ret = new StatDateVO();
            ret.setBeginDate(monthFirst);
            ret.setEndDate(nextMonthFirst);
            return ret;
        }
        return null;
    }

    /**
     * 指定区间的时间区间
     *
     * @param statDateVO {begin YYYY-MM-DD, end YYYY-MM-DD}
     * @return  {YYYY-MM-DD 00:00:00.000, the next day of end YYYY-MM-DD 00:00:00.000 }
     */
    public static StatDateVO getInterval(StatDateVO statDateVO) {
        Date beginDate = str2Date(statDateVO.getBeginDate(), "yyyy-MM-dd");
        Date endDate = str2Date(statDateVO.getEndDate(), "yyyy-MM-dd");
        if (null != beginDate && null != endDate && (beginDate.before(endDate))) {
            String dateZeroHour = date2Str(beginDate, "yyyy-MM-dd") + " 00:00:00.000";
            Date theNext = nextDate(endDate);
            String nextZeroHour = date2Str(theNext, "yyyy-MM-dd") + " 00:00:00.000";
            StatDateVO ret = new StatDateVO();
            ret.setBeginDate(dateZeroHour);
            ret.setEndDate(nextZeroHour);
            return ret;
        }
        return null;
    }

    /**
     * 指定日期的时间区间
     *
     * @param oneDate YYYY-MM-DD
     * @return  {YYYYMMDD, next day YYYYMMDD}
     */
    public static StatDateQry readDateInterval(String oneDate) {
        Date theDate = str2Date(oneDate, "yyyy-MM-dd");
        if (null != theDate) {
            String dateZeroHour = date2Str(theDate, "yyyyMMdd");
            Date theNext = nextDate(theDate);
            String date24Hour = date2Str(theNext, "yyyyMMdd");
            StatDateQry ret = new StatDateQry();
            ret.setBeginDate(Integer.valueOf(dateZeroHour));
            ret.setEndDate(Integer.valueOf(date24Hour));
            return ret;
        }
        return null;
    }

    /**
     * 指定月份的时间区间
     *
     * @param oneMonth YYYY-MM
     * @return  {YYYYMM01, next month YYYYMM01}
     */
    public static StatDateQry readMonthInterval(String oneMonth) {
        Date theDate = str2Date(oneMonth + "-01", "yyyy-MM-dd");
        if (null != theDate) {
            String monthFirst = date2Str(monthFirstDate(theDate), "yyyyMMdd");
            String nextMonthFirst = date2Str(DateUtils.nextMonthFirstDate(theDate), "yyyyMMdd");
            StatDateQry ret = new StatDateQry();
            ret.setBeginDate(Integer.valueOf(monthFirst));
            ret.setEndDate(Integer.valueOf(nextMonthFirst));
            return ret;
        }
        return null;
    }

    /**
     * 指定区间的时间区间
     *
     * @param statDateVO {begin YYYY-MM-DD, end YYYY-MM-DD}
     * @return  {YYYYMMDD, the next day of end YYYYMMDD}
     */
    public static StatDateQry readInterval(StatDateVO statDateVO) {
        Date beginDate = str2Date(statDateVO.getBeginDate(), "yyyy-MM-dd");
        Date endDate = str2Date(statDateVO.getEndDate(), "yyyy-MM-dd");
        if (null != beginDate && null != endDate && (beginDate.before(endDate))) {
            String dateZeroHour = date2Str(beginDate, "yyyyMMdd");
            Date theNext = nextDate(endDate);
            String nextZeroHour = date2Str(theNext, "yyyyMMdd");
            StatDateQry ret = new StatDateQry();
            ret.setBeginDate(Integer.valueOf(dateZeroHour));
            ret.setEndDate(Integer.valueOf(nextZeroHour));
            return ret;
        }
        return null;
    }

    public static void main(String[] args) {
//        System.out.println(getDateInterval("2020-08-09"));
//        System.out.println(getMonthInterval("2020-08-09"));
//        System.out.println(getMonthInterval("2020-08-31"));

        StatDateVO vo = new StatDateVO();
        vo.setBeginDate("2020-08-31");
        vo.setEndDate("2020-09-35");
        System.out.println(getInterval(vo));
    }

}
